var rootPath = getRootPath();
var param = "";

$(function() {
	
	var rleId = getUrlParam("rleId");
	var title = getUrlParam("title");
	var creType;
	
	var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
	
	if(callbackUrl){
		param += "&callbackUrl=" +  encodeURIComponent(callbackUrl);
	}else{
		param += "";
	}
	
	var uid = getUrlParam("uid");//getUrlParam 带转义
	if(uid){
		param += "&uid=" +  encodeURIComponent(uid);
	}else{
		param += "";
	}
	
	$("#basic").html(title + " > 认证结果");
	$.ajax({
		url : rootPath + '/verify/uid/rleId/' + rleId + "/dr/1",
		type : 'GET',
		success : function(data) {
				var verify = data.returnObject;
				if("0000" == data.returnCode) {
				creType = verify.sort;
				var state = verify.state;
				var cName;
				if(creType < 100 && creType != 10) {
					cName = formatData(verify.realNameTmp.realName);
				} else if(creType == 10){
					cName = verify.communityName;
				} else {
					cName = verify.company;	
				}
				var checkResult = verify.checkResult;
				var verifySuccessts = verify.verifySuccessts;
				
				let tip = '';
				let html='<div class="card-wapper">'
					+'<div class="card-left">'
					+'<img alt="" src="../../images/auth/verify_wait.png">'
					+' </div>'
					+'<div class="card-right">'
					+'<ul>'
					+'<li><span class="name">认证类型 ：</span>' + formatData(title) + '</li>'
					+(verify.coachPlanRole != null ? ('<li><span class="name">角色 ：</span>' +  verify.role.rleName) : "")
					+'<li><span class="name">' + (creType < 100 & creType != 10 ? '姓名：' : (creType==10)?'社区名称：':'单位名称：') + '</span>' + cName + '</li>'
					+'	<li><span class="name" >认证成功时间：</span>' + ((verifySuccessts != undefined || verifySuccessts == '') ? verifySuccessts.substring(0,10) : '<span style="color:rgb(197, 24, 10)">审核中</span>')+ '</li>'
					+'</ul>'
					+'</div>';
				let applyDiv='';
				switch(state){
				case 1:
					tip ='<div class="title"><span>正在<icon style="color: #C6180A;">审核中</icon>，工作人员将在3个工作日内完成审核，请耐心等候。</span></div>';
					html+='<label class="status">审核中</label>'
					+'</div>';
					 applyDiv += '<div class="commitBtn" id="editDiv">'+
					'<button class="applyAdream" id="returnRealNameAuth">返回身份类型</button>'+
		            '</div>';
				   $("#result").append(applyDiv);
					break;
				case 2:
//					tip ='<div class="title"><span>恭喜，您已<icon style="color:#2FBEB9">完成真爱梦想身份认证</icon>！如有信息变动可联系管理员！</span></div>';
					tip ='<div class="title"><span>恭喜，您已完成 <icon style="color:#2FBEB9">' + formatData(title) + '</icon>！如有信息变动可 </span><span class="editVerify" id="editAuth">修改认证</span></div>';
					html+='<label class="status status1">审核成功</label>'
						+'</div>';
					applyDiv = '<div class="commitBtn" id="editDiv">' + 
					'<button class="applyAdream" id="returnRealNameAuth">返回身份类型</button>'; 
					//若为'卓越梦想校长报名'
					if(creType == 19) {
						applyDiv += '<button class="download" id="downloadEnrollMaterials">下载报名材料</button>';
					}
					applyDiv += '</div>';
					$("#result").append(applyDiv);
//                    if(verify.rleId == "20181524903016560101"){
//						 applyDiv += '<div class="commitBtn" id="editDiv">'+
//						'<div class="applyInfo">'+
//						'<div class="tips">唯一授权码已发送到该学校校长的手机和邮箱，请向校长索要</div>'+
//						'<view class="giveCode"> <input type="text" placeholder="输入唯一授权码" id="authorizationCode"/>'+
//						'<button class="confirm" onclick="commitAuthorizationCode(\''+verify.school.sid+'\')">提交</button>'+
//						'</view></div>'+
//						
//						'<button class="applyAdream" id="returnRealNameAuth">返回身份类型</button>'+
//						'<button class="applyAdream applyAdream1" id="applyAdream">申请成为梦想中心主任</button>'+
//						
//			            '</div>';
//					   $("#result").append(applyDiv);
//						if(verify.school.directorNum == 1){
//							$("#applyAdream").click(function(){
//								$(".applyInfo").show();
//								$(this).hide();
//							})
//						}else{
//							if(verify.school.uid == verify.uid){
//								$("#applyAdream").css("background","#ddd");
//								var s ='<div class="tips">您已经成为梦想中心主任啦！</div>'+
//								'<div class="tips">如发生变动，请发送邮件至kefu@adream.org</div>'+
//								'</div>';
//								$("#editDiv .applyInfo").show().html(s)
//								$("#applyAdream").hide();
//							
//							} else {
//								$("#editDiv").remove();
//							}
//						}						
//					}
					break;
				case 3:
					tip ='<div class="title"><span>抱歉！您的身份认证信息<icon style="color: #C6180A;">未能通过审核</icon>。原因：' + checkResult + '。请</span> <span class="editVerify" id="editAuth">修改认证</span></div>';
					html='<div class="card-wapper">'
						+'<img class="verify_fail" alt="" src="../../images/auth/verify_fail.png"> <label class="status" >审核失败</label>'
						+'</div>';
					applyDiv += '<div class="commitBtn" id="editDiv">'+
					'<button class="applyAdream" id="returnRealNameAuth">返回身份类型</button>'+
		            '</div>';
				   $("#result").append(applyDiv);
					break;
				//认证过期
				case 4:
					tip ='<div class="title"><span>抱歉！' + checkResult + '。请</span> <span class="editVerify" id="editAuth">重新认证</span></div>';
					html='<div class="card-wapper">'
						+'<img class="verify_fail" alt="" src="../../images/auth/verify_fail.png"> <label class="status" >审核过期</label>'
						+'</div>';
					applyDiv += '<div class="commitBtn" id="editDiv">'+
					'<button class="applyAdream" id="returnRealNameAuth">返回身份类型</button>'+
		            '</div>';
				   $("#result").append(applyDiv);
					break;
				}
				$("#result").prepend(html);
				$("#result").prepend(tip);
				$("#editAuth").click(function() {
					var backId = rleId;
					switch(rleId){
					case "20181529574197910110":
					case "20181529574197910111":
					case "20181529574197910112":
					case "20181529574197910113":
						backId = "goFarAway";
						break;
					case "20201572578634560011":
					case "20201572578634560012":
					case "20201572578634560013":
					case "20201572578634560014":
						backId = "coachPlan";
						break;
					case "20201572578634560016":
						backId = "htc";
						break;
					case "20181529574197910108": //城市社区
					case "20181529574197910120": //乡镇社区
						backId = "adreamCenter";
						break;
					}
					//官方认证 跳转至"实名信息"页
					if(creType > 100) {						
						parent.$("#content").prop("src","realNameAuth.html?rleId=" + backId + "&creType=" + creType);
					} else {
						//个人/团体认证 
						$.ajax({
							url : rootPath + "/realNameAuth/dr/1/uid",
							method : "GET",
							success : function(data) {
								//实名认证存在，且认证中或认证成功
								if('0000' == data.returnCode 
										&& data.returnObject != null
										&& data.returnObject.state < 3) {
									var url = switchPage(creType, rleId);
									parent.$("#content").prop("src", url + '&currentPage=applyResult.html' + param);
								} else {
									//实名认证不存在或实名认证认证失败
									parent.$("#content").prop("src","realNameAuth.html?rleId=" + backId + "&creType=" + creType + param);
								}
							}
						});
					}
				});
				$("#returnRealNameAuth").click(function(){
					parent.$("#content").prop("src","idAuthentication.html");
				});
				$("#downloadEnrollMaterials").click(function(){
					window.open("https://anyshare.aishu.cn/link/AACAD11DB200104B9EAC72B61A092751BD");
				});
			}
		}
	});
});

function formatData(data) {
	return (data != undefined && data !='undefined') ? data : '';
}
function applyButton(sid) {
	$("#applyButton").hide();
	
	var textDiv = $('<div class="centerDiv"><span>唯一授权码已发送到该学校校长的手机和邮箱，请向校长索要</span><div>');
	textDiv.appendTo($("#applyDiv"));
	
	var commitDiv = $('<div class="centerDiv"><input style="border:0.5px solid #aaa" type="text" placeholder="输入唯一授权码" id="authorizationCode">&nbsp;&nbsp;&nbsp;<input type="button" onclick="commitAuthorizationCode(\''+sid+'\')" value="提交"  id="commitButton"></div>');
	commitDiv.appendTo($("#applyDiv"));
	
}
function commitAuthorizationCode(sid){
	var authorizationCode = $("#authorizationCode").val();
	if(authorizationCode==null||authorizationCode==""){
		console.error("授权码空");
		layer.alert("授权码不能为空")
		return;
	}
	$.ajax({
		url : rootPath + "/user/applyDirector",
		type : 'POST',
		dataType: 'json',
        contentType:'application/json;charset=UTF-8',
         data: JSON.stringify({
        	 "authorizationCode":authorizationCode,
        	 "sid":sid
         }),
		success:function(res){
			if(res.returnCode == '0000'){
				layer.msg("申请成功", {
					icon : 1,
					time : 3000
				});
				$(".applyInfo").hide();
				$(".applyAdream").show();
				$(".applyAdream").html("您已经成为梦想中心主任啦！");
				$("#applyAdream").css("background","#ddd");
			}else{
				layer.alert(res.returnMessage);
			}
		},
		error:function(){
			layer.alert("error");
		}
	});	
}